package com.juyu.miliao.api.service;


import com.juyu.miliao.api.dao.mysql.OfficialQuestionToAnswerDao;
import com.juyu.miliao.api.dao.mysql.UserInfoDao;
import com.juyu.miliao.api.dao.redis.OfficialQuestionToAnsweRedis;
import com.juyu.miliao.api.dao.redis.usercache.UserCacheVo;
import com.juyu.miliao.api.dao.redis.usercache.UserInfoCache;
import com.juyu.miliao.api.domain.OfficialQuestionToAnswerInfo;

import com.juyu.miliao.api.domain.vo.OfficialQuestionToAnswerVo;
import com.juyu.miliao.api.rabbitMQ.OfficialQuestionToAnswerRabbitMq;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;


/**
 * @author xuanqicheng
 * @create 2018-18-10:58 AM
 */
@Service
public class OfficialQuestionToAnswerService {

    /**
     * key的格式OfficalQuestionToAnswerNumbers:{官方问题id}
     */

    @Autowired
    private OfficialQuestionToAnswerDao officialQuestionToAnswerDao;

    @Autowired
    private UserInfoDao userInfoDao;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private OfficialQuestionToAnsweRedis officialQuestionToAnsweRedis;

    @Autowired
    private UserInfoCache userInfoCache;

    @Autowired
    private OfficialQuestionToAnswerRabbitMq officialQuestionToAnswerRabbitMq;

    /**
     * 检查用户是否回答过此官方问题,如果用户回答过返还true,没有回答过返还false
     * @param entity
     * @return
     */
    public boolean checkQuestionAnsweringByOfficialQuestionIdAndAnswerUserId(OfficialQuestionToAnswerInfo entity) {

        boolean bl = true;

        OfficialQuestionToAnswerInfo obj = officialQuestionToAnswerDao
                .checkQuestionAnsweringByOfficialQuestionIdAndAnswerUserId(entity);

        if(obj==null){
            bl = false;
        }

        return bl;
    }

    /**
     * 添加数据
     * @param officialQuestionToAnswerInfo
     */
    public void createInfo(OfficialQuestionToAnswerInfo officialQuestionToAnswerInfo) {

        officialQuestionToAnswerInfo.setCreateDate(LocalDate.now());

        officialQuestionToAnswerInfo.setCreateTime(LocalDateTime.now());


        officialQuestionToAnswerDao.createInfo(officialQuestionToAnswerInfo);

        officialQuestionToAnsweRedis.setNewOfficialQuestionToAnswerInfo(officialQuestionToAnswerInfo);

        //更新最新回答人信息，已经前三个回答人的头像
        officialQuestionToAnsweRedis.updateOfficialQuestionToAnswerByIconCache(officialQuestionToAnswerInfo.getOfficialQuestionId());

        //删除缓存
        officialQuestionToAnswerRabbitMq.deleteQfficialAnswerInfo(officialQuestionToAnswerInfo);
    }


    public List<OfficialQuestionToAnswerVo> selectPageInfoByQuestionId(Integer questionId, Integer page, Integer pageSize) {
        List<OfficialQuestionToAnswerVo> resultList = new ArrayList<>(0);
        List<OfficialQuestionToAnswerInfo> list = officialQuestionToAnsweRedis
                .selectPageInfoByQuestionId(questionId,page,pageSize);

        if(list!=null&& list.size()>0){
            for(OfficialQuestionToAnswerInfo officialQuestionToAnswerInfo : list){

                OfficialQuestionToAnswerVo obj = new OfficialQuestionToAnswerVo();

                BeanUtils.copyProperties(officialQuestionToAnswerInfo,obj);

                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM-dd HH:mm");

                String localTime = dateTimeFormatter.format(officialQuestionToAnswerInfo.getCreateTime());

                obj.setShowTime(localTime);

                long number = officialQuestionToAnsweRedis.getEavesdroppingNumber(questionId,officialQuestionToAnswerInfo.getId());

                obj.setEavesdroppingNumber(number);

                UserCacheVo userCacheVo = userInfoCache.getUserCacheInfo(officialQuestionToAnswerInfo.getAnswerUserId());

                if(userCacheVo!=null){
                    obj.setAnswerUserNickName(userCacheVo.getNickName());
                    obj.setAnswerMGrade(userCacheVo.getMGrade());
                    obj.setAnswerVGrade(userCacheVo.getVGrade());
                    obj.setAnswerUserAge(userCacheVo.getAge());
                    obj.setAnswerUserSex(userCacheVo.getSex());
                    obj.setAnswerUserIcon(userCacheVo.getIcon());
                    obj.setAnswerIsVip(userCacheVo.getIsVip());
                    obj.setAnswerIsHost(userCacheVo.getIsHost());
                    obj.setAnswerVipLevel(userCacheVo.getVipLevel());
                    obj.setAnswerStar(userCacheVo.getStar());
                }

                resultList.add(obj);

            }
        }
        return resultList;

    }

    public void updateEavesdroppingNumber(OfficialQuestionToAnswerInfo officialQuestionToAnswerInfo, long userId) {

        officialQuestionToAnsweRedis.updateEavesdroppingNumber(officialQuestionToAnswerInfo,userId);
    }

    public OfficialQuestionToAnswerInfo selectOfficialAnswerById(Integer id) {
        return officialQuestionToAnswerDao.selectOfficialAnswerById(id);
    }
}
